package com.young.shop.dao.impl;

import com.young.shop.dao.UserDao;
import com.young.shop.pojo.entity.User;
import com.young.shop.utils.DBUtil;
import com.young.shop.utils.Utils;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner=new QueryRunner(DBUtil.getDataSource());

    @Override
    public int insert(User user) {
        String sql="INSERT INTO t_user(email,password) VALUES(?,?)";
        Object[] params={user.getEmail(),user.getPassword()};
        try {
            int result = queryRunner.update(sql , params);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public User selectByEmail(String email) {
        String sql="SELECT * FROM t_user WHERE EMAIL=?";
        Object[] params={email};
        RowProcessor processor = Utils.getRowProcessor();
        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class , processor);
        try {
            User dbUser = queryRunner.query(sql , userBeanHandler , params);
            return dbUser;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public User selectByUserName(String userName) {
        String sql="SELECT * FROM t_user WHERE user_name=?";
        Object[] params={userName};
        try {
            //开启驼峰映射
            BeanProcessor bean = new GenerousBeanProcessor();
            RowProcessor processor = new BasicRowProcessor(bean);

            BeanHandler<User> beanHandler = new BeanHandler<>(User.class, processor);
            User dbUser = queryRunner.query(sql,beanHandler,params);
            return dbUser;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public User selectByID(long id) {
        String sql="SELECT * FROM t_user WHERE id=?";
        Object[] params={id};
        try {
            //开启驼峰映射
            BeanProcessor bean = new GenerousBeanProcessor();
            RowProcessor processor = new BasicRowProcessor(bean);

            BeanHandler<User> beanHandler = new BeanHandler<>(User.class, processor);
            User dbUser = queryRunner.query(sql,beanHandler,params);
            return dbUser;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateByID(long id , User user) {
        String sql="UPDATE `t_user` SET `email` = ?, `phone` = ?, `nick_name` = ?, " +
                    "`sex` = ?, `birthday` = ?, `real_name` = ?  WHERE `id` = ?;";
        Object[] params={user.getEmail(),user.getPhone(),user.getNickName(),user.getSex(),user.getBirthday(),user.getRealName(),id};
        try {
            int update = queryRunner.update(sql,params);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int changePasswordByID(long id , String password) {
        String sql="UPDATE `t_user` SET `password` = ?  WHERE `id` = ?;";
        Object[] params={password,id};
        try {
            int update = queryRunner.update(sql,params);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateEmailByID(long id , String email) {
        String sql="UPDATE `t_user` SET `email` = ?  WHERE `id` = ?;";
        Object[] params={email,id};
        try {
            int update = queryRunner.update(sql,params);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }


}
